package com.vivid.system.controller;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import io.netty.util.internal.ObjectUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.vivid.system.domain.User;
import com.vivid.system.service.IUserService;

import javax.servlet.http.HttpSession;


/**
 * 用户信息管理Controller
 *
 * @author vivid
 * @date 2022-08-08
 */
@CrossOrigin
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询用户信息管理列表
     * 验证注册账号唯一性验证
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }


    /**
     * 获取用户信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(userService.selectUserByUserId(userId));
    }

    /**
     * 新增用户信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "用户信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        if(user.getAccount().equals("")||user.getPassword().equals("")){
            return AjaxResult.error("请完整填写账号或密码！");
        }
        //对象为空时
        else if (Objects.isNull(userService.selectUserByUserName(user.getAccount()))){
            return toAjax(userService.insertUser(user));
        }

        return AjaxResult.error("该账号已存在，请更换账号!");
    }

    /**
     * 修改用户信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user, HttpSession session)
    {
        if(user.getAccount().equals("")||user.getPassword().equals("")){
            return AjaxResult.error("请完整填写账号或密码！");
        }
        else if(!Objects.isNull(userService.selectUserByUserName(user.getAccount()))){
            if (userService.selectUserByUserName(user.getAccount()).getUserId().equals(user.getUserId())){
                return toAjax(userService.updateUser(user));
            }
            return AjaxResult.error("该账号已存在，请更换账号!");
        }
        return toAjax(userService.updateUser(user));

    }

    /**
     * 删除用户信息管理
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(userService.deleteUserByUserIds(userIds));
    }
}
